<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"

      :http-request="httpRequest"
    >
      <img v-if="imageUrl" width="100" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
      <el-progress v-if="isShow" type="circle" :percentage="0" />
    </el-upload>
  </div>
</template>
<script>
var COS = require('cos-js-sdk-v5')
var cos = new COS({
  SecretId: 'AKIDvkdM6sZ90kP8iCUiu4BJWikn2ABGMScq', // 身份识别ID
  SecretKey: 'gGeo6JQLHAsaS2gYSQLGB5ucED5qfcYM' // 身份秘钥
})
console.log(cos)
export default {
  props: {
    src: {
      type: String,
      default: ''

    }
  },
  data() {
    return {
      imageUrl: this.src,
      percent: 0,
      isShow: true
    }
  },
  methods: {
    httpRequest(res) {
      this.isShow = true
      // this.imageUrl = URL.createObjectURL(val.file)
      if (res.file) {
        // 执行上传操作
        cos.putObject({
          Bucket: 'wei0217-1315592497', /* 存储桶 */
          Region: 'ap-nanjing', /* 存储桶所在地域，必须字段 */
          Key: res.file.name, /* 文件名 */
          StorageClass: 'STANDARD', // 上传模式, 标准模式
          Body: res.file, // 上传文件对象,
          onProgress: (progressData) => {
            this.percent = progressData.percent * 100
            console.log(this.percent)
          }
        }, (err, data) => {
          console.log(err || data)
          // 上传成功之后
          if (data?.statusCode === 200) {
            this.imageUrl = `https:${data.Location}`
            this.$emit('tupian', this.imageUrl)
            this.isShow = false
          }
        })
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    }

  }}
</script>

